home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6476 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  102 lines

  1. Path: munnari.OZ.AU!metro!metro!OzEmail!usenet
  2. From: rwilson@ozemail.com.au (Ross Wilson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie C question
  5. Date: Sun, 25 Feb 1996 01:40:58 GMT
  6. Organization: OzEmail Pty Ltd - Australia
  7. Message-ID: <4gob7t$8m3@oznet11.ozemail.com.au>
  8. References: <Dn4us9.Mu@undergrad.math.uwaterloo.ca>
  9. NNTP-Posting-Host: slcan1p05.ozemail.com.au
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. sckettle@undergrad.math.uwaterloo.ca (Steve Kettle) wrote:
  13.  
  14. >Hi, how the heck does one convert the error codes found in <errno.h> 
  15. >to strings?  I heard about a perror function but can't find it.
  16.  
  17. >Thanks!
  18. >Steve.
  19. >-- 
  20.  
  21. Steve,
  22.  
  23. there is indeed a perror() function that should be supported by your 
  24. compiler - look in the on-line docs that your compiler may have.
  25.  
  26. My compiler (BC++ 4.52) says:
  27.  
  28. ===================================================
  29. Syntax
  30.  
  31. #include <stdio.h>
  32. void perror(const char *s);
  33.  
  34. Description
  35.  
  36. Prints a system error message.
  37. perror prints to the stderr stream (normally the console) the system 
  38. error message for the last library routine that set the global 
  39. variable errno.
  40. It prints the argument s followed by a colon (:) and the message 
  41. corresponding to the current value of the global variable errno and 
  42. finally a new line. The convention is to pass the file name of the 
  43. program as the argument string.
  44. ===================================================
  45.  
  46. An example (stdio.h, etc included):
  47.  
  48.     char *fname = "file.txt";
  49.     FILE *fp;
  50.  
  51.     fp = fopen(fname, "r"):
  52.     if (fp == NULL)
  53.     {
  54.         perror(fname);
  55.         exit(EXIT_FAILURE);
  56.     }
  57.  
  58.  
  59. I don't use perror() much as I usually want to do more than it 
  60. provides, such as pass an error string to an error handler.  A more 
  61. flexible function is strerror() that does a simple 'errno' to 'string 
  62. describing the error' conversion:
  63.  
  64. ===================================================
  65. Syntax
  66.  
  67. #include <string.h>
  68. char *strerror(int errnum);
  69.  
  70. Description
  71.  
  72. Returns a pointer to an error message string.
  73. strerror takes an int parameter errnum, an error number, and returns a
  74.  
  75. pointer to an error message string associated with errnum.
  76.  
  77. Return Value
  78.  
  79. strerror returns a pointer to a constructed error string. The error 
  80. message string is constructed in a static buffer that is overwritten 
  81. with each call to strerror.
  82. ===================================================
  83.  
  84. An example of use (errno.h, string.h, etc included):
  85.  
  86.     char *fname = "file.txt";
  87.     FILE *fp;
  88.  
  89.     fp = fopen(fname, "r"):
  90.     if (fp == NULL)
  91.     {
  92.         fprintf(stderr, "Can't open file '%s' for input: %s\n",
  93.                         fname, strerror(errno)
  94.                );
  95.         exit(EXIT_FAILURE);
  96.     }
  97.  
  98.  
  99. good luck
  100. Ross
  101.  
  102.